package com.somsoft.unlockme;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.somsoft.unlockme.db.Level;
import com.somsoft.unlockme.fk.C;

public class MainView extends SurfaceView implements SurfaceHolder.Callback{

	
	private Bitmap background;
	private Bitmap lock;
	private Bitmap nolock; 
	private Bitmap leftArrow,rightArrow,whitePoint,yellowPoint;
	
	MainActivity activity;
	MainDrawThread thread;			          //后台重绘线程
	
	public MainView(MainActivity activity) {
		super(activity);
		this.activity = activity;
		getHolder().addCallback(this);	
		background = BitmapFactory.decodeResource(getResources(), R.drawable.choose);
		lock = BitmapFactory.decodeResource(getResources(), R.drawable.lock);
		nolock = BitmapFactory.decodeResource(getResources(), R.drawable.nolock);
		C.star1 = BitmapFactory.decodeResource(getResources(), R.drawable.star1);
		C.star2 = BitmapFactory.decodeResource(getResources(), R.drawable.star2);
		leftArrow = BitmapFactory.decodeResource(getResources(), R.drawable.left_arrow);
		rightArrow = BitmapFactory.decodeResource(getResources(), R.drawable.right_arrow);
		whitePoint = BitmapFactory.decodeResource(getResources(), R.drawable.white_point);
		yellowPoint = BitmapFactory.decodeResource(getResources(), R.drawable.yellow_point);
	}
	
	//起点坐标60,295
	protected void doDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawColor(Color.BLACK);		//清屏
		
		Paint paint = new Paint();		
		paint.setARGB(255, 0, 0, 0);
		paint.setTextSize(50*C.scaleScreen);//设置字体大小
		paint.setColor(Color.rgb(107, 74, 44));
		paint.setTypeface(C.fontNumber);//设置字体类型
		paint.setTextAlign(Align.CENTER); 
		
		Matrix m1 = new Matrix();
		m1.setTranslate(C.leftKongBai, C.topKongBai);
		Matrix m2 = new Matrix();
		m2.setScale(C.scaleBitmap, C.scaleBitmap);
		Matrix m3 = new Matrix();
		m3.setConcat(m1, m2);
		canvas.drawBitmap(background, m3, paint);
		
		//翻页部分
		//左箭头
		if(C.pages.size()==0){
			C.pages.add(new Page("left",C.lockStartX,(int)(C.arrowStartY+10*C.scaleScreen),C.unitArrow));
			C.pages.add(new Page("right",C.rightArrowStartX,(int)(C.arrowStartY+10*C.scaleScreen),C.unitArrow));
		}
		
		m1.setTranslate(C.lockStartX, C.arrowStartY+10*C.scaleScreen);
		m2.setScale(C.scaleBitmap, C.scaleBitmap);
		m3.setConcat(m1, m2);
		canvas.drawBitmap(leftArrow, m3, paint);
		//右箭头
		m1.setTranslate(C.rightArrowStartX, C.arrowStartY+10*C.scaleScreen);
		m2.setScale(C.scaleBitmap, C.scaleBitmap);
		m3.setConcat(m1, m2);
		canvas.drawBitmap(rightArrow, m3, paint);
		//圆点
		for(int i=0;i<5;i++){
			if(C.pages.size()<7){
				C.pages.add(new Page("p"+(i+1),(int)(C.lockStartX+C.unitArrow+C.pointJianGe*(i+1)+C.unitPoint*i),
						(int)(C.arrowStartY+20*C.scaleScreen),C.unitPoint));
			}
			m1.setTranslate(C.lockStartX+C.unitArrow+C.pointJianGe*(i+1)+C.unitPoint*i, C.arrowStartY+20*C.scaleScreen);
			m2.setScale(C.scaleBitmap, C.scaleBitmap);
			m3.setConcat(m1, m2);
			if(C.currPage==(i+1)){
				canvas.drawBitmap(yellowPoint, m3, paint);
			}else{
				canvas.drawBitmap(whitePoint, m3, paint);
			}
		}
		
		
		
		
		int level = 0;
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				if(level >= C.lvs.size()){
					break;
				}
				int tmpX = C.lockStartX + C.unitLock * j + C.jiange * (j + 1);
				int tmpY = C.lockStartY + C.unitLock * i + C.jiange * (i + 1);
				
				Level lv = C.lvs.get(level);//当前要画的关卡对象
				lv.setX(tmpX);
				lv.setY(tmpY);
				if("Y".equals(lv.getIsLock())){
					//锁定的界面
					m1.setTranslate(tmpX, tmpY);
					m3.setConcat(m1, m2);
					canvas.drawBitmap(lock, m3, paint);
				}else{
					//解锁的界面背景
					m1.setTranslate(tmpX, tmpY);
					m3.setConcat(m1, m2);
					canvas.drawBitmap(nolock, m3, paint);
					//数字
					canvas.drawText(lv.getLevel()+"", tmpX+C.unitLock/2, tmpY+55*C.scaleScreen, paint);
					//得分情况，星形
					for(int k=0;k<3;k++){
						m1.setTranslate(tmpX+(C.unitLock-C.unitStar*3)/2+C.unitStar*k, tmpY+C.unitNumber);
						m3.setConcat(m1, m2);
						if(k>=lv.getScore()){
							canvas.drawBitmap(C.star2, m3, paint);
						}else{
							canvas.drawBitmap(C.star1, m3, paint);
						}
					}
				}
				level++;
			}
		}
	}

	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		if(action == MotionEvent.ACTION_UP){
			int tmpPage = C.currPage;
			for(Level lv : C.lvs){
				boolean b = isCollsion(lv.getX(),lv.getY(),C.unitLock,C.unitLock,(int) event.getX(),(int) event.getY());
				if(b && "N".equals(lv.getIsLock())){
					C.selectLevel = lv.getLevel();
					C.currLevel = lv.getLevel();
					break;
				}
			}
			for(Page p : C.pages){
				boolean b = isCollsion(p.getX(),p.getY(),p.getSize(),p.getSize(),(int) event.getX(),(int) event.getY());
				if(b){
					if("left".equals(p.getType())){
						tmpPage--;
						if(tmpPage<1){
							tmpPage = 1;
						}
					}else if("right".equals(p.getType())){
						tmpPage++;
						if(tmpPage>5){
							tmpPage = 5;
						}
					}else{
						tmpPage =Integer.parseInt(p.getType().substring(1,2));
					}
					C.currPage = tmpPage;
					C.isGetLevel = true;
					break;
				}
			}
		}
		return true;
	}
		
	/**
	 * 判断矩形是否与该点碰撞
	 * @param recX
	 * @param recY
	 * @param recWidth
	 * @param recHeight
	 * @param pX
	 * @param pY
	 * @return
	 */
	public boolean isCollsion(int recX, int recY, int recWidth, int recHeight, int pX,int pY){
		if(pX >= recX && pX <= recX+recWidth && pY >= recY && pY <= recY+recHeight){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	  * 矩形碰撞的函数
	  * @param x1 第一个矩形的X坐标
	  * @param y1 第一个矩形的Y坐标
	  * @param w1 第一个矩形的宽
	  * @param h1 第一个矩形的高
	  * @param x2 第二个矩形的X坐标
	  * @param y2 第二个矩形的Y坐标
	  * @param w2 第二个矩形的宽
	  * @param h2 第二个矩形的高
	  */
	public boolean isCollsionWithRect(int x1, int y1, int w1, int h1, int x2,
			int y2, int w2, int h2) {
		// 当矩形1位于矩形2的左侧
		if (x1 >= x2 && x1 >= x2 + w2) {
			return false;
			// 当矩形1位于矩形2的右侧
		} else if (x1 <= x2 && x1 + w1 <= x2) {
			return false;
			// 当矩形1位于矩形2的上方
		} else if (y1 >= y2 && y1 >= y2 + h2) {
			return false;
		} else if (y1 <= y2 && y1 + h1 <= y2) {
			return false;
		}
		// 所有不会发生碰撞都不满足时，肯定就是碰撞了
		return true;
	}
	
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		thread = new MainDrawThread(this,getHolder());
		if(!thread.isAlive()){			//启动后台绘制线程
			Log.i("", "线程启动===============");
			thread.start();
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		if(thread.isAlive()){				//停止后台绘制线程
			thread.flag = false;
			Log.i("", "线程结束===============");
		}	
	}
	
}



















